<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      .box {
        width: 600px;
        border: 10px solid;
        margin: 200px auto auto 200px;
        display: flex;
        position: relative;
        overflow: hidden;
      }

      .box ul {
        padding: 0;
        margin: 0;
        list-style: none;
        background-color: antiquewhite;
        display: flex;
        flex-wrap: nowrap;
        transition: 200ms;
      }
      .box ul li {
        width: 200px;
      }
      .box ul li img {
        width: 100%;
      }

      .btn {
        width: 40px;
        border: none;
        position: absolute;
        top: 0;
        bottom: 0;
        background: rgba(0, 0, 0, 0.1);
        display: flex;
        justify-content: center;
        align-items: center;
        font-size: 2rem;
      }
      .btn:hover {
        background: rgba(0, 0, 0, 0.5);

        color: white;
        cursor: pointer;
      }
      .btn-pre {
        left: 0;
      }
      .btn-next {
        right: 0;
      }

      .pages {
        width: 400px;
        position: absolute;
        bottom: 5px;
        display: flex;
      }
      .pages span {
        background: yellow;
        border: 1px solid;
        width: 20px;
        height: 20px;
        display: flex;
        justify-content: center;
        align-items: center;
      }
    </style>
  </head>
  <body>
    <div id="box" class="box">
      <ul id="ul">
        <li>
          <img src="./imgs/1.png" alt="" />
        </li>
        <li>
          <img src="./imgs/2.png" alt="" />
        </li>
        <li>
          <img src="./imgs/3.png" alt="" />
        </li>
        <li>
          <img src="./imgs/4.png" alt="" />
        </li>
        <li>
          <img src="./imgs/6.png" alt="" />
        </li>
        <li>
          <img src="./imgs/7.png" alt="" />
        </li>
        <li>
          <img src="./imgs/8.png" alt="" />
        </li>
        <li>
          <img src="./imgs/9.png" alt="" />
        </li>
        <li>
          <img src="./imgs/10.png" alt="" />
        </li>
        <li>
          <img src="./imgs/10.png" alt="" />
        </li>
      </ul>
      <button id="preBtn" class="btn btn-pre">⇦</button>
      <button id="nextBtn" class="btn btn-next">⇨</button>
      <div class="pages" id="pagesEL"></div>
    </div>
    <script>
      const box = document.getElementById("box");
      const pagesEL = document.getElementById("pagesEL");
      const preBtn = document.getElementById("preBtn");
      const nextBtn = document.getElementById("nextBtn");
      const ul = document.getElementById("ul");
      const lis = document.querySelectorAll(".box ul li");

      function pages(start, len) {
        // 根据图片的个数创建页数
        pagesEL.innerHTML = [...lis]
          .map((el, i) => {
            if (i >= start && i < start + len) {
              return `<span style="background-color: aqua;">${i + 1}</span>`;
            } else {
              return `<span>${i + 1}</span>`;
            }
          })
          .join("");
      }
      pages(0, 3);
      // ⇦⇨

      // ul.style.marginLeft = "0px";
      // ul.style.marginLeft = `-${(lis.length - 3) * 200}px`;
      // ul.style.marginLeft = "-100px";

      // 图片宽度
      const witdh = 200;
      // 显示图片的个数
      const count = 3;
      // 移动的距离
      let pos = 0;

      // 当前开始在第几页（图片）
      let start = 0;
      nextBtn.onclick = function () {
        pos -= witdh * count;
        pos = Math.max(pos, -(lis.length - 3) * 200);
        ul.style.marginLeft = `${pos}px`;

        // 显示图片位置
        start += count;
        start = Math.min(start, lis.length - count);
        pages(start, count);
      };

      preBtn.onclick = function () {
        pos += witdh * count;
        pos = Math.min(pos, 0);
        ul.style.marginLeft = `${pos}px`;

        // 显示图片位置
        start -= count;
        start = Math.max(start, 0);
        pages(start, count);
      };
    </script>
  </body>
</html>
